<?php

namespace Project\Admin\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;

class AdminUserController extends BaseController
{

    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $maps = [];
        request()->filled('phone') && $maps[] = ['phone', 'like', '%'.request('phone').'%'];
        request()->filled('name') && $maps[] = ['name', 'like', '%'.request('name').'%'];
        request()->filled('status') && $maps[] = ['status', '=', request('status')];
        $data = \Project\Admin\Models\AdminUser::where($maps)->paginate(limit());
        return $this->success(['code' => 200, 'msg' => '成功获取管理员列表.', 'data' => $data]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $params = request()->only(['parent_id', 'name', 'phone', 'password']);
        $rules = [
            'parent_id' => 'sometimes|numberic',
            'name'      => 'required',
            'phone'     => 'required',
            'password'  => 'required',
        ];
        $messages = [
            'parent_id.numberic' => '上级ID数据异常.',
            'name.required'       => '名称不能为空.',
            'phone.required'      => '联系方式不能为空.',
            'password.required'   => '密码不能为空.',
        ];
        $payload = $this->requestValidator($params, $rules, $messages);
        $payload['password'] = bcrypt($payload['password']);
        $payload['parent_id'] = $payload['parent_id'] ?? 0;
        $result = \Project\Admin\Models\AdminUser::create($payload);
        if($result){
            return $this->success('添加管理员:成功.');
        }else{
            return $this->fail('添加管理员:失败.');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = \Project\Admin\Models\AdminUser::where('id', $id)->delete();
        return $this->success('成功删除管理员.');
    }

    public function upload(){
        if(request()->filled('records')){
            $data = request()->records;
            $time = date('Y-m-d H:i:s');
            foreach($data as $key => $value){
                $data[$key]['password']   = bcrypt($value['password']);
                $data[$key]['parent_id']  = $value['parent_id'] ?? 0;
                $data[$key]['created_at'] = $time;
            }
            $result = \Project\Admin\Models\AdminUser::insert($data);
            return $this->success('导入成功.');
        }else{
            return $this->fail('导入数据不能为空.');
        }
    }
    
}
